﻿// -----------------------------------------------------------------------
// <copyright file="ExcelController.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ClearLines.Stocks.UserInterface
{
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using Microsoft.Office.Interop.Excel;
   using Chart = Microsoft.Office.Interop.Excel.Chart;
   using Workbook = Microsoft.Office.Interop.Excel.Workbook;
   using Worksheet = Microsoft.Office.Interop.Excel.Worksheet;

   /// <summary>
   /// TODO: Update summary.
   /// </summary>
   public class ExcelController
   {
      private readonly Application excel;
      private Workbook workbook;

      public ExcelController(Application excel)
      {
         this.excel = excel;
      }

      public Workbook Workbook
      {
         get
         {
            if (this.workbook == null)
            {
               var workbooks = excel.Workbooks;
               this.workbook = workbooks.Add();
            }

            return this.workbook;
         }
      }

      public Worksheet GetWorksheetForSymbol(string symbolToLoad)
      {
         var workbook = this.Workbook;
         var sheets = workbook.Worksheets;
         var worksheets = sheets.OfType<Worksheet>();
         var targetName = SheetNameForSymbol(symbolToLoad);
         var worksheet = worksheets.FirstOrDefault(it => it.Name == targetName);
         if (worksheet == null)
         {
            worksheet = sheets.Add(Type: XlSheetType.xlWorksheet);
            worksheet.Name = targetName;
         }

         return worksheet;
      }

      public Chart AddChart(string name)
      {
         var workbook = this.Workbook;
         var chart = (Chart)workbook.Charts.Add();
         chart.Location(XlChartLocation.xlLocationAsNewSheet, name);
         return chart;
      }

      public Chart GetChartForSymbol(string symbolToLoad)
      {
         var workbook = this.Workbook;
         var sheets = workbook.Charts; 
         var charts = sheets.OfType<Chart>();
         var targetName = ChartNameForSymbol(symbolToLoad);
         var chart = charts.FirstOrDefault(it => it.Name == targetName);

         if (chart == null)
         {
            chart = (Chart) workbook.Charts.Add();
            chart.Location(XlChartLocation.xlLocationAsNewSheet, targetName);
         }

         var seriesCollection = (SeriesCollection)chart.SeriesCollection();
         foreach (Series existingSeries in seriesCollection)
         {
            existingSeries.Delete();
         }

         return chart;
      }

      public Chart GetDensityChartForSymbol(string symbolToLoad)
      {
         var workbook = this.Workbook;
         var sheets = workbook.Charts;
         var charts = sheets.OfType<Chart>();
         var targetName = DensityChartNameForSymbol(symbolToLoad);
         var chart = charts.FirstOrDefault(it => it.Name == targetName);

         if (chart == null)
         {
            chart = (Chart)workbook.Charts.Add();
            chart.Location(XlChartLocation.xlLocationAsNewSheet, targetName);
         }

         var seriesCollection = (SeriesCollection)chart.SeriesCollection();
         foreach (Series existingSeries in seriesCollection)
         {
            existingSeries.Delete();
         }

         return chart;
      }

      private string SheetNameForSymbol(string symbol)
      {
         return symbol + " data";
      }

      private string ChartNameForSymbol(string symbol)
      {
         return symbol + " chart";
      }

      private string DensityChartNameForSymbol(string symbol)
      {
         return symbol + " proj";
      }
   }
}
